import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  // 配置一级路由
  // 登录页
  { path: '/test', component: () => import('@/views/Test.vue') },
  { path: '/login', component: () => import('@/views/Login.vue') },
  // 注册页
  { path: '/register', component: () => import('@/views/Register.vue') },
  // 详情页
  { path: '/detail/:id', component: () => import('@/views/Detail.vue') },
  // 首页
  {
    path: '/',
    component: () => import('@/views/Home.vue'),
    children: [
      // 面经
      { path: 'article', component: () => import('@/views/Layout/Article') },
      // 收藏
      { path: 'collect', component: () => import('@/views/Layout/Collect') },
      // 喜欢
      { path: 'lick', component: () => import('@/views/Layout/Like') },
      // 我的
      { path: 'user', component: () => import('@/views/Layout/User') }
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  // to 到哪里去   to.path  表示即将要访问的那个地址
  // from 从哪里来
  // next() -- 表示放行；    next(false) -- 不允许通过；   next('/login') -- 强行跳转到/login
  if (to.path === '/login' || to.path === '/register') {
    next()
  } else {
    if (localStorage.getItem('mobile-token')) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
